#pragma once
#include "../../src/TcpServer.hpp"

class EchoServer
{
public:
    EchoServer(int port) : server_(port)
    {
        server_.set_thread_count(2);
        server_.enbale_inactive_release(10);

        server_.set_connected_callback(std::bind(&EchoServer::on_connected,this,std::placeholders::_1));
        server_.set_closed_callback(std::bind(&EchoServer::on_closed,this,std::placeholders::_1));
        server_.set_message_callback(std::bind(&EchoServer::on_message,this,std::placeholders::_1, std::placeholders::_2));
    }

    void start(){
        server_.start();
    }
private:
    void on_connected(const Connection::PtrConnection &conn)
    {
        LOG_DEBUG("new 了一个 Connection 对象 : conn_id : {}", conn->get_connect_id());
    }

    void on_message(const Connection::PtrConnection &conn, Buffer *buf)
    {
        LOG_DEBUG("有消息到来 {}", buf->read_string_pop(buf->get_readable_size()));
        std::string response = "hello client !";
        conn->send(response.c_str(), response.size());
        conn->shutdown();
    }

    void on_closed(const Connection::PtrConnection &conn)
    {
        LOG_DEBUG("关闭了一个connection {}", conn->get_connect_id());
    }

private:
    TcpServer server_;
};